package my.algo.graph;

import my.algo.data.LinkedList;

public class EdgeWeightedGraph {

    /** 顶点总数 */
    private final int v;
    /** 边的总数 */
    private int e;
    private final LinkedList<Edge>[] adj;

    public EdgeWeightedGraph(int v) {
        this.v = v;
        this.e = 0;
        adj = (LinkedList<Edge>[]) new LinkedList[v];
        for (int i = 0; i < v; i++) {
            adj[i] = new LinkedList<>();
        }
    }

    public int V() {
        return v;
    }

    public int E() {
        return e;
    }

    public void addEdge(Edge e) {
        int v = e.either(), w = e.other(v);
        adj[v].add(e);
        adj[w].add(e);
        this.e++;
    }

    public Iterable<Edge> adj(int v) {
        return adj[v];
    }

    public Iterable<Edge> edges() {
        LinkedList<Edge> bag = new LinkedList<>();
        for (int i = 0; i < v; i++) {
            for (Edge edge : adj[v]) {
                if (edge.other(v) > i) {
                    bag.add(edge);
                }
            }
        }
        return bag;
    }
}
